import Taro, { Component } from '@tarojs/taro'
import {View,
  Image,
  Text} from '@tarojs/components'
import { AtTabs, AtTabsPane ,AtSwipeAction} from 'taro-ui'

import './myCollections.scss'

import JobItem from '../../component/JobItem/jobItem.js'
import CarItem from '../../component/CarItem/carItem.js'
import ShopItem from '../../component/ShopItem/shopItem.js'
import StoreItem from '../../component/StoreItem/storeItem.js'

import DataManager from '../../utils/DataManager.js'

import ListEmptyView from '../../component/LiseEmptyView/listEmptyView.js'
import emptyIcon from '../../image/ic_empty.png'

import {base_url} from "../../network/config";
import BasePage from "../basePage";
import {COMMON_CONST} from "../../utils/const";

let jobPage = 1;
let carPage = 1;
let shopPage = 1;
let storePage = 1;

export default class myCollections extends BasePage{
  config = {
    navigationBarTitleText: '我的收藏'
  }

  constructor() {
    super(...arguments)
    this.state = {
      jobList:[],
      carList:[],
      shopInfoList:[],
      storeList:[],
      tabCurrent : 0
    }
  }

  componentWillMount () {
    Taro.eventCenter.on('jobPost',(arg) => {
      console.log('job post -----新的招聘来了')
      jobPage = 1;
      this.loadCollectList(jobPage ,1);
    })
  }

  componentDidMount () {
    this.loadCollectList(jobPage ,1);
    this.loadCollectList(carPage ,2);
    this.loadCollectList(shopPage ,3);
    this.loadCollectList(storePage ,4);
  }

  onPullDownRefresh(){
    if (this.state.tabCurrent == 0){
      jobPage = 1;
      this.loadCollectList(jobPage ,1);
    } else if (this.state.tabCurrent == 1) {
      carPage = 1;
      this.loadCollectList(carPage , 2)
    } else if (this.state.tabCurrent == 2){
      shopPage = 1;
      this.loadCollectList(shopPage , 3)
    } else if (this.state.tabCurrent == 3){
      storePage = 1;
      this.loadCollectList(storePage ,4);
    }
  }

  onReachBottom(){
    if (this.state.tabCurrent == 0){
      jobPage ++;
      this.loadCollectList(jobPage ,1);
    } else if (this.state.tabCurrent == 1) {
      carPage ++;
      this.loadCollectList(carPage , 2)
    } else if (this.state.tabCurrent == 2){
      shopPage ++;
      this.loadCollectList(shopPage , 3)
    } else if (this.state.tabCurrent == 3){
      storePage ++;
      this.loadCollectList(storePage ,4);
    }
  }

  //businessType  类型(1为聘，2为车，3为商 , 4为店铺)
  loadCollectList(page , businessType){
    console.log('loadpage------------' + page)

    let _this = this;
    let url = base_url + 'rest/user/collect/list'
    Taro.request({
      url:url,
      data: {
        token:DataManager.getToken(),
        platform:'android_c',
        weixinId:DataManager.getWeiXinId(),
        loginChannel:'c',
        appId:'KC876D98B30UA40CJSNHI689FB33SDW',
        digest : 'dda484557c12febe48139d8f44051c13',
        pageNo: page,
        pageSize: COMMON_CONST.LIST_SIZE,
        businessType:businessType
      },
      method: 'GET',
      success(res) {
        if (!res.data || !res.data.data || !res.data.data.list || res.data.data.list.length < 1){
          if (businessType == 1){
            if (jobPage > 1){
              jobPage --;
            }
          } else if (businessType == 2){
            if (carPage > 1){
              carPage --;
            }
          } else if (businessType == 3){
            if (shopPage > 1){
              shopPage --;
            }
          } else if (businessType == 4){
            if (storePage > 1){
              storePage --;
            }
          }
        }

        let dataList = [];
        if (page != 1){
          if (businessType == 1){
            dataList = _this.state.jobList;
          } else if (businessType == 2){
            dataList = _this.state.carList;
          } else if (businessType == 3){
            dataList = _this.state.shopInfoList;
          } else if (businessType == 4){
            dataList = _this.state.storeList;
          }
        }
        console.log('collectList=========' + res.data.data.list.length)
        dataList = dataList.concat(res.data.data.list);
        if (businessType == 1){
          _this.setState({
            jobList: dataList,
          })
        } else if (businessType == 2){
          _this.setState({
            carList: dataList,
          })
        } else if (businessType == 3){
          _this.setState({
            shopInfoList: dataList,
          })
        } else if (businessType == 4){
          _this.setState({
            storeList: dataList,
          })
        }
      },
      fail(){
        if (businessType == 1){
          if (jobPage > 1){
            jobPage --;
          }
        } else if (businessType == 2){
          if (carPage > 1){
            carPage --;
          }
        } else if (businessType == 3){
          if (shopPage > 1){
            shopPage --;
          }
        } else if (businessType == 4){
          if (storePage > 1){
            storePage --;
          }
        }
      },
      complete(){
        Taro.stopPullDownRefresh();
      }
    })
  }

  handleCancelCollect(itemId , itemType){
    console.log('.itemId......' + itemId + '     ' + itemType)
    let _this = this;
    Taro.request({
      url:base_url + 'rest/user/collect/cancel',
      data:{
        token:DataManager.getToken(),
        platform:'android_c',
        weixinId:DataManager.getWeiXinId(),
        loginChannel:'wechat',
        appId:'KC876D98B30UA40CJSNHI689FB33SDW',
        digest : 'dda484557c12febe48139d8f44051c13',
        businessId:itemId,
        businessType:itemType
      },
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      method: 'POST',
      success(res2) {
        console.log('------cancel  collect=========' + JSON.stringify(res2))
        if (itemType == 1){
          jobPage = 1;
          _this.loadCollectList(jobPage , 1)
        } else if (itemType == 2){
          carPage = 1;
          _this.loadCollectList(carPage , 2)
        } else if (itemType == 3){
          shopPage = 1;
          _this.loadCollectList(shopPage , 3)
        } else if (itemType == 4){
          storePage = 1;
          _this.loadCollectList(storePage , 4)
        }
      }
    })
  }

  onJobCancel = (item , option) => {
    this.handleCancelCollect(item.id , 1)
  }

  onCarCancel = (item , option) => {
    this.handleCancelCollect(item.id , 2)
  }

  onShopCancel = (item , option) => {
    this.handleCancelCollect(item.id , 3)
  }

  onStoreCancel = (item , option) =>{
    this.handleCancelCollect(item.id , 4)
  }

  handleClick = (pos) =>{
    this.setState({
      tabCurrent : pos
    })
  }

  render(){

    let tabList = [{ title: '招聘' }, { title: '找车' }, { title: '商家发布' } , {title : '店铺'}]
    let options = [ { text: '取消关注', style: { backgroundColor: '#FF4949' } }]

    return(
      <View className='my-collection-container'>
        <AtTabs swipeable={false} current={this.state.tabCurrent} tabList={tabList} onClick={this.handleClick.bind(this)}>
          <AtTabsPane current={this.state.tabCurrent} index={0}>
            {this.state.jobList.length > 0 ? this.state.jobList.map(item => {
              return (<AtSwipeAction autoClose  onClick={this.onJobCancel.bind(this , item)} options={options}>
                  <JobItem jobData={item}></JobItem>
                </AtSwipeAction>
                )
            })
            :
            <ListEmptyView emptyText='暂无数据' emptyImage={emptyIcon}/>
            }
          </AtTabsPane>
          <AtTabsPane current={this.state.tabCurrent} index={1}>
            {this.state.carList.length > 0 ? this.state.carList.map(item => {
              return (<AtSwipeAction autoClose  onClick={this.onCarCancel.bind(this , item)} options={options} >
                  <CarItem carData={item}></CarItem>
                </AtSwipeAction>
              )
            })
            :
            <ListEmptyView emptyText='暂无数据' emptyImage={emptyIcon}/>
            }
          </AtTabsPane>
          <AtTabsPane current={this.state.tabCurrent} index={2}>
            {this.state.shopInfoList.length > 0 ? this.state.shopInfoList.map(item => {
              return (<AtSwipeAction autoClose  onClick={this.onShopCancel.bind(this , item)} options={options}>
                  <ShopItem shopData={item}></ShopItem>
                </AtSwipeAction>
              )
            })
            :
            <ListEmptyView emptyText='暂无数据' emptyImage={emptyIcon}/>
            }
          </AtTabsPane>
          <AtTabsPane current={this.state.tabCurrent} index={3}>
            {this.state.storeList.length > 0 ? this.state.storeList.map(item => {
                return (<AtSwipeAction autoClose  onClick={this.onStoreCancel.bind(this , item)} options={options}>
                    <StoreItem storeData={item}></StoreItem>
                  </AtSwipeAction>
                )
              })
              :
              <ListEmptyView emptyText='暂无数据' emptyImage={emptyIcon}/>
            }
          </AtTabsPane>
        </AtTabs>
      </View>
    )
  }

}

